package com.example.viewpagertest3;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity
{
	private MyViewPager myViewPager;
	private MyPagerAdapter myPagerAdapter;
	
	private int ADAPTER_COUNT = 700000;
	private int prevPosition = ADAPTER_COUNT / 2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		this.myPagerAdapter = new MyPagerAdapter(this.getSupportFragmentManager());	
        
		this.myViewPager = (MyViewPager)findViewById(R.id.pager);
		this.myViewPager.setPagingEnabled(true);
		this.myViewPager.setAdapter(this.myPagerAdapter);
		this.myViewPager.setCurrentItem(this.ADAPTER_COUNT / 2, false);
	}

	private class MyPagerAdapter extends FragmentStatePagerAdapter
	{
		public MyPagerAdapter(FragmentManager fm)
		{
			super(fm);
		}

		@Override
		public int getCount()
		{
			return ADAPTER_COUNT;
		}

		@Override
		public Fragment getItem(int position)
		{
			MyFragment myFragment = new MyFragment();
            
            if(prevPosition == position)
            	Log.d("111111111111", "Application Loading!!!!!!!!!!");
            else if(prevPosition < position)
            	Log.d("111111111111", "Next Move!!!!!!!!!!!!!");
            else
            	Log.d("111111111111", "Prev Move!!!!!!!!!!!!!");
            
            Bundle args = new Bundle();
            args.putInt("position", position);
            
            myFragment.setArguments(args);
            
            Log.d("11111111111", "getItem.position : " + position + " , prevPosition : " + prevPosition);
            
            prevPosition = position;
            
            return myFragment;
		}
	}
	
	private class MyFragment extends Fragment 
	{
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
		{
			View view = inflater.inflate(R.layout.sample_textview, container, false);
			
			Bundle args = this.getArguments();
			int position = args.getInt("position");
			
			TextView tv = (TextView)view.findViewById(R.id.textView1);
			tv.setText(String.valueOf(position));
			
			if(position % 3 == 2)
				tv.setBackgroundColor(Color.GREEN);
			else if(position % 3 == 1)
				tv.setBackgroundColor(Color.YELLOW);
			else
				tv.setBackgroundColor(Color.LTGRAY);
			
			return view;
		}
	}
}























